/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.request;

import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import database.ProductDAO;
import database.RequestDAO;
import database.StaffDAO;
import java.util.HashMap;
import java.util.Map;
import model.Product;
import model.Request;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Request")
public class CreateReceipRequestAction extends ActionSupport{
    
    private Integer pid;
    
    private Product product;
    
    private String reason;
    private String message;
    
    private Staff staff;
    private int quantity;
    
    private Request request;
    
    public RequestDAO requestDAO = new RequestDAO();

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    
    public Staff getStaff() {
        return staff;
    }

    public void setStaff(Staff staff) {
        this.staff = staff;
    }
    
    public String getReason() {
        return reason;
    }

    public void setReason(String reason) {
        this.reason = reason;
    }
    
    

    public Product getProduct() {
        return product;
    }

    public void setProduct(Product product) {
        this.product = product;
    }
      

    public Integer getPid() {
        return pid;
    }

    public void setPid(Integer pid) {
        this.pid = pid;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public Request getRequest() {
        return request;
    }

    public void setRequest(Request request) {
        this.request = request;
    }
      @Action(value = "create-request", results = {
        @Result(name = "success", location = "/manageRequest/w_result.jsp"),
        @Result(name = "input", location = "/manageRequest/w_requestReceipt.jsp")
    })
    @Override
    public String execute() throws Exception {
        //System.out.println("PAsss 1");
        String result;
        Map session = ActionContext.getContext().getSession();
        staff = (Staff) session.get("staff"); 
        product = (Product) session.get("RequestProduct");
        //System.out.println(product);
        if(reason.length() == 0)
        {
            addActionError("Request reason is required.");
            result = INPUT;
        }
        else if (quantity == 0)
        {
            addActionError("Request quantity is required.");
            result = INPUT;
        }
        else
        {
            System.out.println(product);
            Staff approver = new StaffDAO().getStaffByStaffRole("Director");
            request = new Request(staff, approver, reason, product, quantity);
            if(requestDAO.addReceip(request))
            {
                message = "Your request is created.";
                result =  SUCCESS;
                
            }
            else
            {
                addActionError("Add Failed. There are some system errors. Please add again.");
                result = INPUT;
            }
        }     
        return result;
    }
      
       @Action(value = "save-draff", results = {
        @Result(name = "success", location = "/manageRequest/w_result.jsp"),
        @Result(name = "input", location = "/manageRequest/w_requestReceipt.jsp")
    })
    public String SaveDraff() {
        //System.out.println("PAsss 1");
        String result;
        Map session = ActionContext.getContext().getSession();
        staff = (Staff) session.get("staff"); 
        product = (Product) session.get("RequestProduct");
        System.out.println(product);
        Staff approver = new StaffDAO().getStaffByStaffRole("Director");
        request = new Request(staff, approver, reason, product, quantity);
        if(requestDAO.addDraffReceip(request))
        {
            result =  SUCCESS;
            message = "Your request is saved in draff.";
        }
        else
        {
            addActionError("Add Failed. There are some system errors. Please add again.");
            result = INPUT;
        }    
        return result;
    }
//    @Override
//    @VisitorFieldValidator(message = "", appendPrefix = false)
//    public Request getModel() {
//        return request; //To change body of generated methods, choose Tools | Templates.
//    }
}
